﻿using HalconDotNet;
using MvCamCtrl.NET;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using YTE.Control;

namespace LD.Vision
{
    public partial class FrmCamSet : FrmBase
    {
        public FrmCamSet()
        {
            InitializeComponent();
            //float maxVal = float.MaxValue;
            //float minVal = float.MinValue;
            //num_exposure.Maximum = (decimal)maxVal;
            //num_exposure.Minimum = (decimal)minVal;
            //num_gain.Maximum = (decimal)maxVal;
            //num_gain.Minimum = (decimal)minVal;
            //num_frameRate.Maximum = (decimal)maxVal;
            //num_frameRate.Minimum = (decimal)minVal;
            this.Shown += FrmCam_Shown;
        }

        private void FrmCam_Shown(object sender, EventArgs e)
        {
            foreach (var item in CameraFactory.Instance.CameraList)
            {
                this.cob_camera.Items.Add(item.Key);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog file = new OpenFileDialog())
            {
                file.Filter = "所有图像文件 | *.bmp;*.png; *.jpg; *.gif; *.tiff;";
                //             file.Filter = "所有图像文件 | *.bmp; *.pcx; *.png; *.jpg; *.gif;" +
                //"*.tiff; *.ico; *.dxf; *.cgm; *.cdr; *.wmf; *.eps; *.emf";
                file.Title = "打开图像文件";
                file.ShowHelp = true;
                file.Multiselect = false;

                if (file.ShowDialog() == DialogResult.OK)
                {
                    string filename = file.FileName;

                    HImage hImage = new HImage();
                    hImage.ReadImage(filename);
                    //显示图
                    //hWindow_Final1.HobjectToHimage(hImage);
                }
            }
        }

        private void cob_camera_SelectedIndexChanged(object sender, EventArgs e)
        {
            string cameraName = this.cob_camera.Text;

            //num_exposure.ValueChanged -= num_exposure_ValueChanged;
            //num_gain.ValueChanged -= num_gain_ValueChanged;
            //num_frameRate.ValueChanged -= num_frameRate_ValueChanged;
            if (CameraFactory.Instance.CameraList.ContainsKey(cameraName))
            {
                this.sw_saveImage.Checked = CameraFactory.Instance.CameraList[cameraName].IsSaveImage;
            }
            //num_exposure.ValueChanged += num_exposure_ValueChanged;
            //num_gain.ValueChanged += num_gain_ValueChanged;
            //num_frameRate.ValueChanged += num_frameRate_ValueChanged;
        }

        private void num_exposure_ValueChanged(object sender, EventArgs e)
        {
            string cameraName = this.cob_camera.Text;
            if (CameraFactory.Instance.CameraList.ContainsKey(cameraName))
                CameraFactory.Instance.CameraList[cameraName].Exposure = (float)this.num_exposure.Value;
        }

        private void num_gain_ValueChanged(object sender, EventArgs e)
        {
            string cameraName = this.cob_camera.Text;
            if (CameraFactory.Instance.CameraList.ContainsKey(cameraName))
                CameraFactory.Instance.CameraList[cameraName].Gain = (float)this.num_gain.Value;
        }

        private void num_frameRate_ValueChanged(object sender, EventArgs e)
        {
            string cameraName = this.cob_camera.Text;
            if (CameraFactory.Instance.CameraList.ContainsKey(cameraName))
                CameraFactory.Instance.CameraList[cameraName].FrameRate = (float)this.num_frameRate.Value;
        }
        private void num_gamma_ValueChanged(object sender, EventArgs e)
        {
            string cameraName = this.cob_camera.Text;
            if (CameraFactory.Instance.CameraList.ContainsKey(cameraName))
                CameraFactory.Instance.CameraList[cameraName].Gamma = (float)this.num_gamma.Value;
        }
        private void btn_open_Click(object sender, EventArgs e)
        {
            string cameraName = this.cob_camera.Text;
            int index = this.cob_camera.SelectedIndex;
            if (CameraFactory.Instance.CameraList.ContainsKey(cameraName))
            {
                CameraFactory.Instance.CameraList[cameraName].Open(index);

                this.num_exposure.Value = (decimal)CameraFactory.Instance.CameraList[cameraName].Exposure;
                this.num_gain.Value = (decimal)CameraFactory.Instance.CameraList[cameraName].Gain;
                this.num_frameRate.Value = (decimal)CameraFactory.Instance.CameraList[cameraName].FrameRate;
                this.num_gamma.Value = (decimal)CameraFactory.Instance.CameraList[cameraName].Gamma;


                this.btn_open.Enabled = false;
                this.btn_startGrab.Enabled = true;
                this.btn_closed.Enabled = true;
                this.rab_continuesMode.Enabled = true;
                this.rab_triggerMode.Enabled = true;
            }
        }

        private void btn_closed_Click(object sender, EventArgs e)
        {
            string cameraName = this.cob_camera.Text;
            int index = this.cob_camera.SelectedIndex;
            if (CameraFactory.Instance.CameraList.ContainsKey(cameraName))
                CameraFactory.Instance.CameraList[cameraName].Close();
            this.btn_open.Enabled = true;
            this.btn_startGrab.Enabled = false;
            this.btn_stopGrab.Enabled = false;
            this.btn_closed.Enabled = false;
            this.rab_continuesMode.Enabled = false;
            this.rab_triggerMode.Enabled = false;
        }

        private void btn_startGrab_Click(object sender, EventArgs e)
        {

            rab_continuesMode_CheckedChanged(null, null);
            rab_triggerMode_CheckedChanged(null, null);

            string cameraName = this.cob_camera.Text;
            int index = this.cob_camera.SelectedIndex;
            if (CameraFactory.Instance.CameraList.ContainsKey(cameraName))
            {
                CameraFactory.Instance.CameraList[cameraName].StartGrab();

                this.btn_startGrab.Enabled = false;
                this.btn_stopGrab.Enabled = true;

                this.rab_continuesMode.Enabled = false;
                this.rab_triggerMode.Enabled = false;
            }
        }

        private void btn_stopGrab_Click(object sender, EventArgs e)
        {
            string cameraName = this.cob_camera.Text;
            int index = this.cob_camera.SelectedIndex;
            if (CameraFactory.Instance.CameraList.ContainsKey(cameraName))
            {
                CameraFactory.Instance.CameraList[cameraName].StopGrab();
                this.btn_stopGrab.Enabled = false;
                this.btn_startGrab.Enabled = true;

                this.rab_continuesMode.Enabled = true;
                this.rab_triggerMode.Enabled = true;
            }
        }

        private void btn_saveImage_Click(object sender, EventArgs e)
        {

        }

        private void rab_triggerMode_CheckedChanged(object sender, EventArgs e)
        {
            if (rab_triggerMode.Checked)
            {
                string cameraName = this.cob_camera.Text;
                int index = this.cob_camera.SelectedIndex;
                if (CameraFactory.Instance.CameraList.ContainsKey(cameraName))
                {
                    CameraFactory.Instance.CameraList[cameraName].TriggerMode = true;
                    this.btn_triggerOne.Enabled = true;
                }
            }
        }

        private void rab_continuesMode_CheckedChanged(object sender, EventArgs e)
        {
            if (rab_continuesMode.Checked)
            {
                string cameraName = this.cob_camera.Text;
                int index = this.cob_camera.SelectedIndex;
                if (CameraFactory.Instance.CameraList.ContainsKey(cameraName))
                {
                    CameraFactory.Instance.CameraList[cameraName].ContinuesMode = true;
                }
                this.btn_triggerOne.Enabled = false;
            }
        }

        private void btn_triggerOne_Click(object sender, EventArgs e)
        {
            string cameraName = this.cob_camera.Text;
            int index = this.cob_camera.SelectedIndex;
            if (CameraFactory.Instance.CameraList.ContainsKey(cameraName))
            {
                CameraFactory.Instance.CameraList[cameraName].Trigger();
                this.btn_triggerOne.Enabled = true;
            }
        }

        private void sw_saveImage_CheckedChanged(object sender, EventArgs e)
        {
            string cameraName = this.cob_camera.Text;
            int index = this.cob_camera.SelectedIndex;
            if (CameraFactory.Instance.CameraList.ContainsKey(cameraName))
            {
                CameraFactory.Instance.CameraList[cameraName].IsSaveImage = this.sw_saveImage.Checked;
            }
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
    }
}
